from tkinter import Tk
from tkinter import ttk
from database.table import tablee
from database.table.tablee import Purchases,Commodity

def CountPurchase():
    count_purchase_screen = Tk()
    count_purchase_screen.title('统计采购信息')
    screen_width, screen_height = count_purchase_screen.maxsize()
    # 窗口的宽和高
    width = 700
    height = 200
    centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                              (screen_height - height) / 2)
    count_purchase_screen.geometry(centre)
    count_purchase_screen.resizable(0, 0)

    purchasee = tablee.session.query(Purchases).all()
    data = Purchases.to_json(purchasee)
    length = len(data)

    tree = ttk.Treeview(count_purchase_screen)  # 创建表格对象
    tree["columns"] = ("编号", "员工号", "商品号", "单价", "数量", "总价", "时间", "备注")
    tree.column("编号", width=50)  # 设置列
    tree.column("员工号", width=50)
    tree.column("商品号", width=50)
    tree.column("单价", width=50)
    tree.column("数量", width=100)
    tree.column("总价", width=50)
    tree.column("时间", width=100)
    tree.column("备注", width=50)
    tree.heading("编号", text="编号")  # 显示表头名
    tree.heading("员工号", text="员工号")
    tree.heading("商品号", text="商品号")
    tree.heading("单价", text="单价")
    tree.heading("数量", text="数量")
    tree.heading("总价", text="总价")
    tree.heading("时间", text="时间")
    tree.heading("备注", text="备注")

    cnum = data[0]['Cno']
    comm = tablee.session.query(Commodity).filter(Commodity.Cno == cnum).all()
    commdityy = Commodity.to_json(comm)
    unitprice = commdityy[0]['Cunitprice']

    j = length - 1

    for i in range(0, length):

        cnum = data[j]['Cno']
        comm = tablee.session.query(Commodity).filter(Commodity.Cno == cnum).all()
        commdityy = Commodity.to_json(comm)
        unitprice = commdityy[0]['Cunitprice']

        pno = data[j]['Pno']
        eno = data[j]['Eno']
        cno = data[j]['Cno']
        num = data[j]['Pnum']
        time = data[j]['Pdate']
        remarks = data[j]['Premarks']
        totalp = int(num) * int(unitprice)
        tree.insert("", 0, text='', value=(pno, eno, cno, unitprice, num, str(totalp), time, remarks))
        j = j - 1
    tree.pack()

    tablee.session.close()

    count_purchase_screen.mainloop()

if __name__ == '__main__':
    CountPurchase()
